/**
 * STYLE TASKS
 * @Author: jiangcheng198745@hotmail.com
 * @Date: 2016/07/09
 *
 */
'use strict';

import gulp from 'gulp';
import {argv as args} from 'yargs';
import jsonImporter from 'node-sass-json-importer';
import {stream} from 'browser-sync';
import {init, write} from 'gulp-sourcemaps';
import plumber from 'gulp-plumber';
import {log, colors} from 'gulp-util';
import sass from 'gulp-sass';
import autoprefixer from 'gulp-autoprefixer';
import rename from 'gulp-rename';
import $if from 'gulp-if';
import print from 'gulp-print';


import meesageLogger from '../utils/message.gulp';
import config from '../gulp.config';

// Sass build task
gulp.task('scss:build', ['clean:css'], () => {
    meesageLogger('Compiling SCSS --> CSS');

    //config.appFiles.scss
    return gulp.src('app/styles/app.scss')
        .pipe(init())
        .pipe(plumber((error) => {
            log(colors.red(error));
        }))
        .pipe(sass({
            outputStyle: 'expanded',
            importer: jsonImporter
        }))
        .pipe(autoprefixer({
            browser: ['last 5 versions', '> 5%', 'ie8']
        }))
        .pipe(rename({
            dirname: config.css,
            basename: config.cssName,
            extname: '.css'
        }))
        .pipe($if(args.verbose, print()))
        .pipe(write('/'))
        .pipe(gulp.dest(config.build))
        .pipe(stream({
            match: '**/*.css'
        }))
        .pipe(plumber.stop());
});

gulp.task('watch:scss', () => {
    if (!args.dist) {
        gulp.watch([config.app + '/**/*.scss'], ['scss:build']);
    }
});
